/**
 * 
 */
package com.googlecode.lohr.parser.combinators.matcher;


import com.googlecode.lohr.parser.combinators.MatchError;
import com.googlecode.lohr.parser.combinators.node.StringNode;

public final class RangeMatcher extends Matcher
{
	private final char to;
	private final char from;

	public RangeMatcher(char to, char from)
	{
		this.to = to;
		this.from = from;
	}

	@Override
	public void match(MatchContext ctx, int start) 
	{
		String input= ctx.getInput();
		if (start < input.length()) 
		{
			char c= input.charAt(start);
			for (char ch = from; ch <= to; ch++)
			{
				if (c == ch) {
					ctx.matchFound(this, start, new StringNode(this, start, start+1, "" + ch));
					ctx.matchComplete(this, start);
					return;
				}
			}
		}
		
		ctx.matchComplete(this, start, new MatchError("Expected character in range {'"+from+"' to '"+to+"}", start));
	}
}